#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <aio.h>
void async_read_file(const char *filename) {
    int fd = open(filename, O_RDONLY);
    if (fd == -1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    struct aiocb aiocb;
    memset(&aiocb, 0, sizeof(aiocb));
    aiocb.aio_fildes = fd;
    aiocb.aio_buf = malloc(1024);
    aiocb.aio_nbytes = 1024;
    aiocb.aio_offset = 0;
    // aiocb.aio_flags = AIO_NONBLOCK;

    ssize_t ret = aio_read(&aiocb);
    while ((ret = aio_error(&aiocb)) == EINPROGRESS) {
        usleep(1000); // 等待一段时间再次检查
    }
    if (ret == -1) {
        perror("aio_read");
        exit(EXIT_FAILURE);
    }

    printf("Read %zd bytes from file %s\n", ret, filename);

    close(fd);
}

int main(){
    async_read_file("test.txt");
    return 0;
}
